import type { FormItemRule } from 'element-plus'

import { domainRules } from './domain'

export const useRules = () => {
  return computed<Record<string, FormItemRule[]>>(() => ({
    domains: [...domainRules],
    conditions: [
      {
        trigger: 'blur',
        required: true,
        validator(_, value) {
          if (Array.isArray(value)) {
            const item = value.some(v => v.value)
            if (!item) {
              return new Error('请添加条件')
            }
          }
          return true
        }
      }
    ],
    expr: [
      {
        required: true,
        trigger: 'blur',
        validator(_, value) {
          if (!value.value) {
            const msg = value.key === 'name' ? '请输入主机记录' : '请输入记录值'
            return new Error(msg)
          }
          return true
        }
      }
    ]
  }))
}
